const common = {
    request,
    checkPhoneNumber,
    stringHidden,
    timeChage,
    toast,
    openFile,
    getCountDown,
}

function request(options, db, eb, cb) {
    let url = options.url || ''
    let method = options.type || 'GET'
    let data = options.params || ''
    let loading = options.loading || true
    if (loading) {
        uni.showLoading({
            title: options.wMsg || '加载中...'
        })
    }
    uni.request({
        url: url,
        method: method,
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        data,
        success: function (data) {
            if (data.data.code === '0000') {
                if (typeof db === 'function') return db(data.data.data)
            } else {
                setTimeout(() => {
                    common.toast(data.data.message || '网络异常，请稍后再试～')
                }, 200)
                if (data.data.code === 401) {
					uni.clearStorageSync()
					uni.reLaunch({
						url: '/pages/login'
					})
                }
            }
        },
        fail: () => {
            if (eb) return eb()
        },
        complete: () => {
            if (loading) uni.hideLoading()
            if (cb) return cb()
        }
    })
}

function checkPhoneNumber(mobile) {
    let _mobile = mobile.replace(/\s+/g, "")
    if (!_mobile) {
        common.toast('手机号不能为空')
        return false
    }
    var reg = /^(1[1-9][0-9])\d{8}$/i
    if (!reg.test(_mobile)) {
        common.toast('手机号格式不正确')
        return false
    }
    return true
}

function toast(msg) {
    uni.showToast({
        title: msg,
        icon: 'none',
        position: 'bottom'
    })
}

function stringHidden(num) {
    let len = num.length, h = '****', s, e, c;
    if (len > 1) {
        if (len > 10) {
            s = num.substring(0, 3);
            e = num.substring(len - 4, len);
            c = s + h + e;
            return c;
        } else {
            h = '*'
            s = num.substring(0, 1);
            c = s + h;
            return c;
        }
    } else return num
}

function timeChage(time) {
    let unixTimestamp = new Date(time);
    let _time = unixTimestamp.toLocaleString();
    return _time;
}

Date.prototype.toLocaleString = function () {
    return this.getFullYear() + "-" + ("0" + (this.getMonth() + 1)).slice(-2) + "-" + ("0" + (this.getDate())).slice(-2) + ' ' + ("0" + (this.getHours())).slice(-2) + ":" + ("0" + (this.getMinutes())).slice(-2) + ":" + ("0" + (this.getSeconds())).slice(-2);
}

function openFile(url) {
    if (!url) {
        common.toast('文件地址不可为空');
        return
    }
    uni.showLoading({
        title: '文件加载中...'
    })
    uni.downloadFile({
        url: url,
        success: function (res) {
            var filePath = res.tempFilePath;
            uni.openDocument({
                filePath: filePath,
                success: function (res) {
                    uni.hideLoading()
                }
            })
        }
    })
}

function getCountDown(t) {
    if (!t) return
    let time = t.replace(/-/g, '/')
    let endTime = new Date(time).getTime()
    let currentTime = new Date().getTime()
    let diff = endTime - currentTime
    let days = parseInt(diff / 1000 / 60 / 60 / 24, 10); //天数
    let hours = parseInt(diff / 1000 / 60 / 60 % 24, 10); //小时
    let minutes = parseInt(diff / 1000 / 60 % 60, 10);//分钟
    let seconds = parseInt(diff / 1000 % 60, 10);//秒数
    return {days, hours, minutes, seconds}
}

export default common